// pages/me/pages/importRc/index.js
import {
	input
} from '../../../../utils/tools'
import {
	getAge
} from '../../../../utils/util'
const {
	http
} = require("../../../../utils/apiutils")
import { nation} from './nation.js'
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		type: 1,
		viewType: 1, //  1 ocr识别  2 nfc识别 
		idcardInfo: {
			"talentName": "",
			"talentId": "",
			"talentPhone": "",
			"intention": "",
			"remark": "",
			addr: '',
			record: ''
		},
		_this: null,
		useNfc: '',
		nfcStr: '暂未开启',
		clickNfc:false

	},
	input(e) {
		input.bind(this)(e)
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		this.setData({
			_this: this,
			useNfc: getApp().globalData.userInfo.realName
		})
		
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {
		let _this = this;
		return false;
		wx.getHCEState({
			success(res) {
				let str = "";
				switch (res.errCode) {
					case 0:
						_this.setData({
							clickNfc:true
						})
						break;
					case 13000:
						str = "当前设备不支持NFC";
						break;
					case 13001:
						str = "当前设备支持NFC，但系统NFC开关未开启";
						break;
					default:
						str = "暂未开启";
						break;
				}
				_this.setData({
					nfcStr: str
				})
			},
			fail(err) {
				console.log('错误提示nfc', err)
			}
		})
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	},
	changeType(e) {
		let viewType = 1;
		if (e.currentTarget.dataset.key == 1) {
			return false;
			viewType = 2
		}
		if (viewType==2&&!this.data.clickNfc) { 
			return false;
		}
		this.setData({
			type: viewType == 1 ? 2 : 4,
			viewType
		})
	},
	backdata(data) {
		this.testRepeat(data)
	},
	testRepeat(data) {
		let params = {
			idcard: data.detail.idNumber
		}
		let _this = this;
		console.log(data)
		http.exists(params, res => {
			if (res.status == 200) {
				_this.setData({
					type: 3,
					['idcardInfo.talentName']: data.detail.realName,
					['idcardInfo.gender']: data.detail.sex == '男' ? 1 : 2,
					['idcardInfo.nation']: data.detail.nation,
					['idcardInfo.talentId']: data.detail.idNumber,
					['idcardInfo.age']: getAge(data.detail.idNumber),
					['idcardInfo.addr']: data.detail.address,
					['idcardInfo.validity']: data.detail.validity,
				})
			} else {
				_this.setData({
					type:0
				})
				wx.showModal({
					title: '操作',
					content: '该人才已存在，是否编辑？',
					cancelText: '否',
					confirmText: '是',
					success(res2) {
						if (res2.confirm) {
							_this.setData({
								type: 3,
								['idcardInfo.talentName']: res.obj.talentName,
								['idcardInfo.talentId']: res.obj.talentId,
								['idcardInfo.id']: res.obj.id,
								['idcardInfo.talentPhone']: res.obj.talentPhone,
								['idcardInfo.intention']: res.obj.intention,
								['idcardInfo.record']: res.obj.record,
								['idcardInfo.validity']: res.obj.validity||data.detail.validity,
								['idcardInfo.nation']: data.detail.nation,
								['idcardInfo.age']: getAge(data.detail.idNumber),
								['idcardInfo.gender']: data.detail.sex == '男' ? 1 : 2,
								['idcardInfo.addr']: data.detail.address
							})
						} else {
							_this.setData({
								type: _this.data.viewType==1?2:4,
								idcardInfo: {}
							})
						}

					}

				})
			}
		}, err => {

		})
	},
	next() {
		wx.showLoading()
		console.log('idcardInfo',this.data.idcardInfo)
		http.talentPoolSave(this.data.idcardInfo, res => {
			wx.hideLoading()
			this.setData({
				type: this.data.viewType==1?2:4,
				idcardInfo: {}
			})
			wx.showToast({
				title: res.msg,
				icon: "none"
			})
		}, err => {})
	},
	pushover() {
		wx.showLoading()
		console.log('idcardInfo',this.data.idcardInfo)
		http.talentPoolSave(this.data.idcardInfo, res => {
			wx.hideLoading()
			wx.navigateBack({
				delta: 1,
			})
		}, err => {})
	},
	handlerReading(data) {
		const {
			success,
			error,
			status,
			percent,
			cardInfo
		} = data.detail
		if (success) {
			// 读卡状态和进度
			console.log('-reading process-', status, percent)
			if (status === 3) {
				// 读卡成功
				console.log('读取成功', cardInfo)
				let data = {
					detail: {
						address: cardInfo.address,
						realName: cardInfo.name,
						nation: nation[cardInfo.nation * 1 - 1],
						sex: cardInfo.sex == 1 ?'男':'女',
						idNumber: cardInfo.number,
						validity: cardInfo.begin+'-'+cardInfo.end,
					}
				}
				this.testRepeat(data)
			}
		} else {
			// 读卡失败
			// console.log('-reading error-', error)
			wx.showToast({
				title: error,
				icon:"none"
			})
		}
	},
})
